Merge pull request #536 from cantino/twitter_is_optional_too

twitter agents can be disabled too

Andrew Cantino 10 years ago
parent
commit
1a512dafae

+ 5 - 3
Gemfile

@@ -15,6 +15,11 @@ gem "google-api-client"           # GoogleCalendarPublishAgent
15 15
 gem 'mqtt'                        # MQTTAgent
16 16
 gem 'slack-notifier', '~> 0.5.0'  # SlackAgent
17 17
 
18
+# Twitter Agents
19
+gem 'twitter', '~> 5.8.0' # Must to be loaded before cantino-twitter-stream.
20
+gem 'cantino-twitter-stream', github: 'cantino/twitter-stream', branch: 'master'
21
+gem 'omniauth-twitter'
22
+
18 23
 # Optional Services.
19 24
 gem 'omniauth-37signals'          # BasecampAgent
20 25
 # gem 'omniauth-github'
@@ -31,8 +36,6 @@ gem 'protected_attributes', '~>1.0.8' # This must be loaded before some other ge
31 36
 gem 'ace-rails-ap', '~> 2.0.1'
32 37
 gem 'bootstrap-kaminari-views', '~> 0.0.3'
33 38
 gem 'bundler', '>= 1.5.0'
34
-gem 'twitter', '~> 5.8.0' # needs to be loaded before cantino-twitter-stream.
35
-gem 'cantino-twitter-stream', github: 'cantino/twitter-stream', branch: 'master'
36 39
 gem 'coffee-rails', '~> 4.0.0'
37 40
 gem 'daemons', '~> 1.1.9'
38 41
 gem 'delayed_job', '~> 4.0.0'
@@ -59,7 +62,6 @@ gem 'mysql2', '~> 0.3.16'
59 62
 gem 'multi_xml'
60 63
 gem 'nokogiri', '~> 1.6.1'
61 64
 gem 'omniauth'
62
-gem 'omniauth-twitter'
63 65
 gem 'rails' , '4.1.5'
64 66
 gem 'rufus-scheduler', '~> 3.0.8', require: false
65 67
 gem 'sass-rails',   '~> 4.0.0'

+ 8 - 0
app/concerns/twitter_concern.rb

@@ -6,6 +6,8 @@ module TwitterConcern
6 6
 
7 7
     validate :validate_twitter_options
8 8
     valid_oauth_providers 'twitter'
9
+
10
+    gem_dependency_check { defined?(Twitter) && has_oauth_configuration_for?('twitter') }
9 11
   end
10 12
 
11 13
   def validate_twitter_options
@@ -41,4 +43,10 @@ module TwitterConcern
41 43
       config.access_token_secret = twitter_oauth_token_secret
42 44
     end
43 45
   end
46
+
47
+  module ClassMethods
48
+    def twitter_dependencies_missing
49
+      "## Include the `twitter`, `omniauth-twitter`, and `cantino-twitter-stream` gems in your Gemfile to use Twitter Agents."
50
+    end
51
+  end
44 52
 end

+ 1 - 2
app/models/agents/twitter_publish_agent.rb

@@ -1,5 +1,3 @@
1
-require "twitter"
2
-
3 1
 module Agents
4 2
   class TwitterPublishAgent < Agent
5 3
     include TwitterConcern
@@ -7,6 +5,7 @@ module Agents
7 5
     cannot_be_scheduled!
8 6
 
9 7
     description <<-MD
8
+      #{twitter_dependencies_missing if dependencies_missing?}
10 9
       The TwitterPublishAgent publishes tweets from the events it receives.
11 10
 
12 11
       To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.

+ 1 - 0
app/models/agents/twitter_stream_agent.rb

@@ -5,6 +5,7 @@ module Agents
5 5
     cannot_receive_events!
6 6
 
7 7
     description <<-MD
8
+      #{twitter_dependencies_missing if dependencies_missing?}
8 9
       The TwitterStreamAgent follows the Twitter stream in real time, watching for certain keywords, or filters, that you provide.
9 10
 
10 11
       To follow the Twitter stream, provide an array of `filters`.  Multiple words in a filter must all show up in a tweet, but are independent of order.

+ 1 - 2
app/models/agents/twitter_user_agent.rb

@@ -1,5 +1,3 @@
1
-require "twitter"
2
-
3 1
 module Agents
4 2
   class TwitterUserAgent < Agent
5 3
     include TwitterConcern
@@ -7,6 +5,7 @@ module Agents
7 5
     cannot_receive_events!
8 6
 
9 7
     description <<-MD
8
+      #{twitter_dependencies_missing if dependencies_missing?}
10 9
       The TwitterUserAgent follows the timeline of a specified Twitter user.
11 10
 
12 11
       To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.

+ 1 - 0
bin/threaded.rb

@@ -17,6 +17,7 @@ def safely(&block)
17 17
   rescue StandardError => e
18 18
     STDERR.puts "\nException #{e.message}:\n#{e.backtrace.join("\n")}\n\n"
19 19
     STDERR.puts "Terminating myself ..."
20
+    STDERR.flush
20 21
     stop
21 22
   end
22 23
 end

+ 8 - 1
lib/twitter_stream.rb

@@ -1,6 +1,5 @@
1 1
 require 'cgi'
2 2
 require 'json'
3
-require 'twitter/json_stream'
4 3
 require 'em-http-request'
5 4
 require 'pp'
6 5
 
@@ -88,6 +87,14 @@ class TwitterStream
88 87
   SEPARATOR = /[^\w_\-]+/
89 88
 
90 89
   def run
90
+    if Agents::TwitterStreamAgent.dependencies_missing?
91
+      STDERR.puts Agents::TwitterStreamAgent.twitter_dependencies_missing
92
+      STDERR.flush
93
+      return
94
+    end
95
+
96
+    require 'twitter/json_stream'
97
+
91 98
     while @running
92 99
       begin
93 100
         agents = Agents::TwitterStreamAgent.active.all